﻿using System;
using System.Collections.Generic;
using System.Text;
using Newtonsoft.Json;
using System.Collections.Concurrent;
using System.Xml;

public class StringPlus
    {

    public static string ReplaceHtmlTag(string html){
        string strText = System.Text.RegularExpressions.Regex.Replace(html, "<[^>]+>", "");
        strText = System.Text.RegularExpressions.Regex.Replace(strText, "&[^;]+;", "").Trim();
        return strText;
    }

    public static List<string> GetStrArray(string str, char speater,bool toLower)
        {
            List<string> list = new List<string>();
            string[] ss =  str.Split(speater);
            foreach (string s in ss)
            {
                if (!string.IsNullOrEmpty(s) &&s !=speater.ToString())
                {
                    string strVal = s;
                    if (toLower)
                    {
                        strVal = s.ToLower();
                    }
                    list.Add(strVal);
                }
            }
            return list;
        }
        public static string[] GetStrArray(string str)
        {
            return str.Split(new char[',']);
        }
        public static string GetArrayStr(List<string> list,string speater)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < list.Count; i++)
            {
                if (i == list.Count - 1)
                {
                    sb.Append(list[i]);
                }
                else
                {
                    sb.Append(list[i]);
                    sb.Append(speater);
                }
            }
            return sb.ToString();
        }
        
        
        #region 删除最后一个字符之后的字符

        /// <summary>
        /// 删除最后结尾的一个逗号
        /// </summary>
        public static string DelLastComma(string str)
        {
            return str.Substring(0, str.LastIndexOf(","));
        }

        /// <summary>
        /// 删除最后结尾的指定字符后的字符
        /// </summary>
        public static string DelLastChar(string str,string strchar)
        {
            return str.Substring(0, str.LastIndexOf(strchar));
        }

        #endregion

        


        /// <summary>
        /// 转全角的函数(SBC case)
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string ToSBC(string input)
        {
            //半角转全角：
            char[] c = input.ToCharArray();
            for (int i = 0; i < c.Length; i++)
            {
                if (c[i] == 32)
                {
                    c[i] = (char)12288;
                    continue;
                }
                if (c[i] < 127)
                    c[i] = (char)(c[i] + 65248);
            }
            return new string(c);
        }        

        /// <summary>
        ///  转半角的函数(SBC case)
        /// </summary>
        /// <param name="input">输入</param>
        /// <returns></returns>
        public static string ToDBC(string input)
        {
            char[] c = input.ToCharArray();
            for (int i = 0; i < c.Length; i++)
            {
                if (c[i] == 12288)
                {
                    c[i] = (char)32;
                    continue;
                }
                if (c[i] > 65280 && c[i] < 65375)
                    c[i] = (char)(c[i] - 65248);
            }
            return new string(c);
        }

        public static List<string> GetSubStringList(string o_str, char sepeater)
        {
            List<string> list = new List<string>();
            string[] ss = o_str.Split(sepeater);
            foreach (string s in ss)
            {
                if (!string.IsNullOrEmpty(s) && s != sepeater.ToString())
                {
                    list.Add(s);
                }
            }
            return list;
        }


        #region 将字符串样式转换为纯字符串
        public static string GetCleanStyle(string StrList, string SplitString)
        {
            string RetrunValue = "";
            //如果为空，返回空值
            if (StrList == null)
            {
                RetrunValue = "";
            }
            else
            {
                //返回去掉分隔符
                string NewString = "";
                NewString = StrList.Replace(SplitString, "");
                RetrunValue = NewString;
            }
            return RetrunValue;
        }
        #endregion

        #region 将字符串转换为新样式
        public static string GetNewStyle(string StrList, string NewStyle, string SplitString, out string Error)
        {
            string ReturnValue = "";
            //如果输入空值，返回空，并给出错误提示
            if (StrList == null)
            {
                ReturnValue = "";
                Error = "请输入需要划分格式的字符串";
            }
            else
            {
                //检查传入的字符串长度和样式是否匹配,如果不匹配，则说明使用错误。给出错误信息并返回空值
                int strListLength = StrList.Length;
                int NewStyleLength = GetCleanStyle(NewStyle, SplitString).Length;
                if (strListLength != NewStyleLength)
                {
                    ReturnValue = "";
                    Error = "样式格式的长度与输入的字符长度不符，请重新输入";
                }
                else
                {
                    //检查新样式中分隔符的位置
                    string Lengstr = "";
                    for (int i = 0; i < NewStyle.Length; i++)
                    {
                        if (NewStyle.Substring(i, 1) == SplitString)
                        {
                            Lengstr = Lengstr + "," + i;
                        }
                    }
                    if (Lengstr != "")
                    {
                        Lengstr = Lengstr.Substring(1);
                    }
                    //将分隔符放在新样式中的位置
                    string[] str = Lengstr.Split(',');
                    foreach (string bb in str)
                    {
                        StrList = StrList.Insert(int.Parse(bb), SplitString);
                    }
                    //给出最后的结果
                    ReturnValue = StrList;
                    //因为是正常的输出，没有错误
                    Error = "";
                }
            }
            return ReturnValue;
        }
        #endregion

    }

    public static class ObjConversion
    {
    public static string ToBase64(this string input) {
        byte[] bt = System.Text.Encoding.UTF8.GetBytes(input);
        return Convert.ToBase64String(bt);
    }

        /// <summary>
        /// XML字符串转化为对象列表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="R"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
    public static List<R> XmlStr2List<R>(this string obj)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(obj.Replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", ""));
            string json = Newtonsoft.Json.JsonConvert.SerializeXmlNode(doc);
            if (string.IsNullOrWhiteSpace(json))
            {
                return null;
            }
            else
            {
                return JsonConvert.DeserializeObject<List<R>>(json);
            }
        }

        /// <summary>
        /// XML字符串转实体
        /// </summary>
        /// <typeparam name="R"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static R XmlStr2Entity<R>(this string obj)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(obj.Replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", ""));
            string json = Newtonsoft.Json.JsonConvert.SerializeXmlNode(doc);
            return JsonConvert.DeserializeObject<R>(json);
        }
    public static string DbToString(this object obj)
        {
            if (obj == DBNull.Value || obj == null)
            {
                return "";
            }
            else
            {
                return obj.ToString();
            }
        }

        /// <summary>
        /// json字符串转化为对象列表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="R"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static List<R> Str2List<R>(this string obj)
        {
            if (string.IsNullOrWhiteSpace(obj))
            {
                return null;
            }
            else
            {
                return JsonConvert.DeserializeObject<List<R>>(obj);
            }
        }

        public static R Str2Entity<R>(this string obj)
        {
            return JsonConvert.DeserializeObject<R>(obj);
        }

        /// <summary>
        /// 转换Json
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string ToJson(this object obj)
        {
            if (obj == null || obj == DBNull.Value)
            {
                return "{}";
            }
            else
            {
                return JsonConvert.SerializeObject(obj, Newtonsoft.Json.Formatting.Indented, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
            }
        }



        /// <summary>
        /// 清空ConcurrentQueue
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="queue"></param>
        public static void Clear<T>(this ConcurrentQueue<T> queue)
        {
            T item;
            while (queue.TryDequeue(out item))
            {
                // do nothing
            }
        }

        /// <summary>
        /// UTC转ISO时间
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string UtcToIso(this DateTime dt) {
            return dt.ToString("yyyy-MM-ddTHH:mm:ss.fffffffzzz");
        }

        /// <summary>
        /// ISO时间转UTC时间
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static DateTime? IsoToUtc(this string str) {
            if (string.IsNullOrWhiteSpace(str)) {
                return null;
            }
            return Convert.ToDateTime(str.Replace("T", " ").Replace("Z", "")).AddHours(8);
        }

        /// <summary>
        /// 转换int
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static int GetInt(this object obj)
        {
            if (obj == null || obj == DBNull.Value)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 转换int
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static long GetLong(this object obj)
        {
            if (obj == null || obj == DBNull.Value)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt64(obj);
            }
        }
        /// <summary>
        /// 转换stirng
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string GetString(this object obj)
        {
            if (obj == null || obj == DBNull.Value)
            {
                return string.Empty;
            }
            else
            {
                return obj.ToString();
            }
        }
        /// <summary>
        /// 转换stirng
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static Double GetDouble(this object obj)
        {
            if (obj == null || obj == DBNull.Value||!obj.IsDouble())
            {
                return 0;
            }
            else
            {
                return Convert.ToDouble(obj);
            }
        }
        public static bool IsDouble(this object obj)
        {
            Double i = 0;
            if (Double.TryParse(obj.ToString(), out i))
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 转换stirng
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static Decimal GetDecimal(this object obj)
        {
            if (obj == null || obj == DBNull.Value)
            {
                return 0;
            }
            else
            {
                return Convert.ToDecimal(obj);
            }
        }
        /// <summary>
        /// 转换stirng
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static Decimal GetDecimal(this object obj,Decimal defaultValue)
        {
            if (obj == null || obj == DBNull.Value)
            {
                return defaultValue;
            }
            else
            {
                return Convert.ToDecimal(obj);
            }
        }
        public static bool IsDecimal(this object obj)
        {
            Decimal i = 0;
            if (Decimal.TryParse(obj.ToString(), out i))
            {
                return true;
            }
            return false;
        }
        public static bool IsInt(this object obj)
        {
            int i = 0;
            if (int.TryParse(obj.ToString(),out i))
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 转换stirng
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static bool GetBool(this object obj)
        {
            if (obj == null || obj == DBNull.Value)
            {
                return false;
            }
            else
            {
                return Convert.ToBoolean(obj);
            }
        }
        
    }
